package client;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import client.domain.Visitor;

public class WrapNetViVi {

	final Logger logger = LoggerFactory.getLogger(WrapNetViVi.class);

	private static final int PORT = 5555;      // server details
	private static final String HOST = "localhost";
	private Socket sock;
	private BufferedReader in;     // IO for the client
	private PrintWriter out;  
	private DecimalFormat df;   // for simpler output

	private List<Visitor> visitors = new ArrayList<Visitor>();

	public void makeContact()
	/* Contact the server, and set up a TourWatcher to monitor the
	     server. */
	{
		logger.info("Make contact");
		try {

			sock = new Socket(HOST, PORT);
			in  = new BufferedReader( 
					new InputStreamReader( sock.getInputStream() ) );
			out = new PrintWriter( sock.getOutputStream(), true );  // autoflush

			out.println("create " + "toto" + " "+0+" "+0);
			new TourWatcher(this, in).start();    // start watching for server msgs
		}
		catch(Exception e)
		{ System.out.println("No contact with server");
		System.exit(0);
		}
	}  // end of makeContact()

	public void addVisitor(Visitor v){
		visitors.add(v);
	}

	private void createSceneGraph(String userName, String tourFnm,
			double xPosn, double zPosn) 
	//	initilise the scene
	{ 
		makeContact();     // contact server
		addVisitor(new Visitor());      
	} // end of createSceneGraph()

	public void sendDetails(String cliAddr, String strPort)
	/* Send details of local sprite to the client at location 
	    (cliAddr, strPort).
	     Msg format:  
	         detailsFor cliAddr strPort xPosn zPosn rotRadians
	 */
	{  

		String msg = new String("detailsFor " + cliAddr + " " +
				strPort);
		out.println(msg);
		logger.info("Message sent {}",msg);
	}  // end of sendDetails()

	public static void main(String [] args){


		WrapNetViVi wrapNetViVi = new WrapNetViVi();
		wrapNetViVi.makeContact();
		for(int i = 0; i<5; i++){
			wrapNetViVi.sendDetails("null", "1234");
			try {
				Thread.sleep(1000);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
		while(true);
		
	}

}


